package com.bdqn.crm.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.crm.pojo.SalChance;
import com.bdqn.crm.pojo.SysRole;
import com.bdqn.crm.pojo.SysUser;
import com.bdqn.crm.service.SalChanceService;
import com.bdqn.crm.service.SysUserService;
import com.bdqn.crm.utils.DataFormat;
import com.bdqn.crm.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chance")
public class ChanceController {

    @Resource
    private SalChanceService salChanceService;

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model,@RequestParam(value = "custName",required = false) String custName,@RequestParam(value = "chcDesc",required = false) String chcTitle, @RequestParam(required = false, defaultValue = "1") int pageIndex){
        Page<SalChance> page = new Page<>(pageIndex, 5);
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();
        //判断是否有查询条件
        if (custName != null && custName.trim() != "") {
            queryWrapper.like("chc_cust_name", custName);      //模糊设置条件
        }
        if (chcTitle != null && chcTitle.trim() != "") {
            queryWrapper.like("chc_title", chcTitle);
        }
        if (salChanceService.count(queryWrapper) == 0) {     //此操作说明 数据库没有数据可展示，那么就全部清零
            page.setTotalElements(0);
            page.setContent(null);
        } else {
            page.setTotalElements(salChanceService.count(queryWrapper));
            page.setContent(salChanceService.selectPages(custName, chcTitle, (page.getNumber() - 1) * page.getPageSize(), page.getPageSize()));
        }
        model.addAttribute("custName",custName);
        model.addAttribute("chcDesc",chcTitle);
        model.addAttribute("chancePage", page);
        return "chance/list";
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam("chcId") Long chcId,Model model,HttpServletRequest request){
        SalChance salChance = salChanceService.getById(chcId);
        salChance.setChcCreateDate(DataFormat.forMat(salChance.getChcCreateDate()));
        salChance.setChcDueDate(DataFormat.forMat(salChance.getChcDueDate()));

        System.out.println(JSON.toJSONString(salChance));
        List<SysUser> users = sysUserService.list();
        model.addAttribute("chance",salChance);
        model.addAttribute("users",users);
        return "chance/edit";
    }

    @PostMapping("/save")
    public String editSave(HttpServletRequest request,SalChance salChance){
        SysUser creteUser = (SysUser) request.getSession().getAttribute("loginUser");
        salChance.setChcCreateId(creteUser.getUsrId());
        salChance.setChcCreateBy(creteUser.getUsrName());
        salChance.setChcDueTo(creteUser.getUsrName());
        salChanceService.saveOrUpdate(salChance);
        return "redirect:/chance/list";
    }

    @ResponseBody
    @GetMapping(value = "/del/{chcId}")
    public String del(Map<String,String> map, @PathVariable("chcId") Long chcId){
        if(chcId!=null){
            if(salChanceService.removeById(chcId)){
                map.put("delResult","true");
            }else{
                map.put("delResult","false");
            }
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/add")
    public String toAdd(Model model){
        model.addAttribute("users",sysUserService.list());
        return "chance/add";
    }


}
